#!/bin/bash

VER=$1
FILE_VER=$VER
FILE_NAME=$FILE_VER.tar.gz
SETUP_DIR="~/deploy"

NAME=$VER.tar.gz
echo "======tar the $NAME"

tar cjf $FILE_NAME ./$VER

SETUP=./update.$VER.sh


cat > $SETUP <<EOF
#!/bin/bash

echo "======update: $FILE_VER"

rm -rf $SETUP_DIR/$FILE_NAME $SETUP_DIR/$FILE_VER
cp ./$FILE_NAME $SETUP_DIR

cd $SETUP_DIR

echo "======extracting: $FILE_VER"

tar xf ./$FILE_NAME
cd $FILE_VER

GRAPHDB=\`docker image ls --format "{{.Repository}}" | grep graphdb\`
if [[ \$GRAPHDB ]]
then
    cd front
else
    cd time
fi

./update
EOF

chmod +x $SETUP

rm -rf ~/$VER
mkdir -p ~/$VER

cp $NAME $SETUP ~/$VER
hostname -I
